﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runda.CMS.Common.Entity;
using Runda.CMS.Dal;
using Runda.CMS.Common.Utility;
using System.Transactions;
using System.Data;

namespace Runda.CMS.Bll
{
    public class StockBll:BllBase
    {
        public string GetAccessoryCode()
        {
            return new CommonBll().getCode("accessoryCode");
        }

        public string GetInBillCode() {
            return new CommonBll().getCode("accessoryStockIn");
        }

        public string GetOutBillCode()
        {
            return new CommonBll().getCode("accessoryStockOut");
        }

        public DataTable QueryPODetail(String POID)
        {
            return new StockDal().QueryPODetail(POID);
        }
        public DataTable QueryStockInDetail(String StockInID) {
            return new StockDal().QueryStockInDetail(StockInID);
        }        
        public DataTable getStockcount(String AccessoryID)//库存数量
        {
            return new StockDal().getStockcount(AccessoryID);

        }

        public DataTable QueryStockOutDetail(String OutID)
        {
            return new StockDal().QueryStockOutDetail(OutID);
        }

        /// <summary>
        /// 获取个人库存数量
        /// </summary>
        /// <param name="AccessoryID"></param>
        /// <param name="MaintainID"></param>
        /// <returns></returns>
        public decimal GetPersonalStockQuantity(Guid AccessoryID, Guid MaintainID)
        {
            decimal PersonalQuantity = 0;
            WsmMaintainBillEntity BillEntity = base.Get<WsmMaintainBillEntity>(MaintainID);

            if (BillEntity != null)
            {
                if (BillEntity.Maintainer != Guid.Empty)
                {
                    StockDal dal = new StockDal();
                    DataTable dt = dal.GetStockView(" and AccessoryID = '"+AccessoryID+"' and Receiver = '"+BillEntity.Maintainer+"' ");

                    if(dt != null && dt.Rows.Count >0)
                    {
                        PersonalQuantity = Convert.ToDecimal(dt.Rows[0]["PersonalStockQuantity"].ToString());
                    }
                }
            }
            return PersonalQuantity;
        }

        /// <summary>
        /// 获取服务单的配件
        /// </summary>
        /// <param name="WhereClause"></param>
        /// <returns></returns>
        public DataTable GetAccessoryReplacement(string WhereClause)
        {
            WsmAccessoryInfoDal dal = new WsmAccessoryInfoDal();

            return dal.GetAccessoryReplacement(WhereClause);
        }

        public DataTable GetStockView(string WhereClause)
        {
            StockDal dal = new StockDal();
            return dal.GetStockView(WhereClause);
        }

        public DataTable QueryAccessoryStock(string whereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return new StockDal().QueryAccessoryStock(whereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }
        //采购订单
        public DataTable QueryAccessoryPO(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return new StockDal().QueryAccessoryPO(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }
        public DataTable QueryAccessoryPOMaster(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return new StockDal().QueryAccessoryPOMaster(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public DataTable QueryAccessoryPO(string WhereClause)
        {
            return new StockDal().QueryAccessoryPO(WhereClause);
        }

        //公司配件库存
        public DataTable QueryAccessoryStockByCo(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return new StockDal().QueryAccessoryStockByCo(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }
        public DataTable ExportAccessoryStockByCo(string WhereClause, string OrderBy)
        {
            return new StockDal().ExportAccessoryStockByCo(WhereClause, OrderBy);
        }
        

        //库存明细
        public DataTable QueryAccessoryStockDetail(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return new StockDal().QueryAccessoryStockDetail(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }
        //个人配件库存
        public DataTable QueryAccessoryStockByPersonal(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            return new StockDal().QueryAccessoryStockByPersonal(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public DataTable CalcAccessoryStockByCo(string WhereClause)
        {
            return new StockDal().CalcAccessoryStockByCo(WhereClause);
        }

        public DataTable CalcAccessoryStockByPersonal(string WhereClause)
        {
            return new StockDal().CalcAccessoryStockByPersonal(WhereClause);
        }

        public DataTable CalcAccessoryStock(string WhereClause)
        {
            return new StockDal().CalcAccessoryStock(WhereClause);
        }

        //删除服务单时，相应的配件的状态还原（个人库存-1；公司库存-0)
        public void cancelAccessoryUsage(string maintainID)
        {
            new StockDal().cancelAccessoryUsage(maintainID);
        }

        //关闭采购订单明细
        public void ClosePOS(string POSID)
        {
            new StockDal().ClosePOS(POSID);
        }

        //审核采购单
        public void UpdatePOcheckStatus(Guid POID, string checkStatus, string checkMan, DateTime Checktime)
        {
            new StockDal().UpdatePOcheckStatus(POID, checkStatus, checkMan, Checktime);
        }

        //还入配件
        public void BackAccessory(string stockID, string BackMan, DateTime Backdate)
        {
            new StockDal().BackAccessory(stockID, BackMan, Backdate);
        }
        //通过物品识别号获得所在库存ID
        public string getStockid(string accessoryCode)
        {
            string sql="select stockid from Cms_Wsm_AccessoryStock where AccessoryCode='"+accessoryCode+"'";
            DataTable dt = new DataTable();
            dt = new CommonBll().getDataFromSQL(sql);
            return dt.Rows[0]["stockid"].ToString();
        }

        public DataTable ExportAccessoryStockByPersonal(string sqlwhere, string OrderBy)
        {
            string sql = @"select distinct u.UserName as [姓名]
                        ,CompanyInstrumentCode as [配件编号（公司）]
                        ,AccessoryName as [配件名称],FactoryInstrumentCode as [厂家编号],AccessoryStandard as [规格],Unit as [单位],v.Vendor as [厂家],PresellPrice as [销售价]
                        ,isnull(PersonalStockquantity,0) as [当前库存数量] ,v.coStockquantity as [公司仓库数量],v.AccessoryType as [类型]
                        from vw_StockQuery v
                        inner join Cms_Sys_User u with(nolock) on u.UserID = v.Receiver
                        where 1=1 {0} order by {1}";
            sql = string.Format(sql, sqlwhere, OrderBy);
            DataTable dt = new DataTable();
            dt = new CommonBll().getDataFromSQL(sql);
            return dt;
        }
        public DataTable ExportAccessoryStock(string sqlwhere, string OrderBy)
        {
            string sql = @"select distinct AccessoryTypeName as [类型]
                        ,CompanyInstrumentCode as [编号（公司）]
                        ,AccessoryName as [名称]
                        ,vendor as [厂家]
                        ,FactoryInstrumentCode as [厂家代码]
                        ,AccessoryStandard as [规格]
                        ,AccessoryCode as [识别号]
                        ,AccessoryStatusName as [所处位置]
                        ,ReceiverName as [领用人]
                        ,InStockdate as [入库日期] 
                        ,OutStockDate as [借出日期]
                        ,PreBackdate as [预计还入日期]
                        ,backdate as [还入日期]
                        ,AccessoryUsedDate as [使用日期]
                        ,case OrderCheckStatus when 0 then '已保存' when 1 then '已提交' when 2 then '已核查' when 3 then '已下定' else '' end as [服务单状态]
                        from vw_StockDetail
                        where AccessoryCode is not null {0} order by {1} ";
            sql = string.Format(sql, sqlwhere, OrderBy);
            DataTable dt = new DataTable();
            dt = new CommonBll().getDataFromSQL(sql);
            return dt;
        }

        //采购订单
        public DataTable ExportPOList(string WhereClause, string OrderBy)
        {
            return new StockDal().ExportPOList(WhereClause, OrderBy);
        }
        //入库明细
        public DataTable ExportAccessoryStockList(string WhereClause, string OrderBy)
        {
            return new StockDal().ExportAccessoryStockList(WhereClause, OrderBy);
        }

        //入库明细
        public DataTable ExportAccessoryStockOutList(string WhereClause, string OrderBy)
        {
            return new StockDal().ExportAccessoryStockOutList(WhereClause, OrderBy);
        }
    }
}
